<template>
  <div>
    <div>
      <span>异常关闭&nbsp;&nbsp;&nbsp;&nbsp;</span>
      <span style="color: #888" v-if="model.submitdate">{{ model.username }}{{ formatDate(model.submitdate) }}填写</span>
    </div>
    <el-form class="tl-form taskForm" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="disableEditSubmit || disableSave">
      <el-row>
        <el-col :span="24">
          <el-form-item label="关闭原因" prop="closereason" :rules="rules.validateRequire">
            <el-input v-model="model.closereason" type="textarea" :autosize="{ minRows: 1, maxRows: 3 }" maxlength="1000"> </el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <tl-approval-history ref="history" sourceType="MpmProjectTaskClose" :sourceId="model.id" />
    <div slot="footer" class="dialog-footer" style="float: right;margin-right: 10px">
      <el-button icon='el-icon-delete' type="primary" @click="deleteFeedbackList" :loading="saveLoading" v-if="canDelete">删除</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="!disableEditSubmit" :disabled="disableSave"> <svg-icon icon-class="save" />保存 </el-button>
      <tl-attachment sourceType="MpmProjectTaskClose" :sourceId="model.id" :disabled="disableAttachmentButton"></tl-attachment>
      <el-button type="primary" @click="doSubmitApproval" :loading="submitLoading" v-if="!disableEditSubmit" :disabled="disableApproval"> <svg-icon icon-class="send" />提交申请 </el-button>
      <el-button type="primary" @click="$refs.history.visible = true" :disabled="disableApprovalHistory"> <svg-icon icon-class="list" />审批历史 </el-button>
    </div>
  </div>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
import { formatDate } from '@tapui/utils/date'
import { validateNull } from '@tapui/utils/validate'
/** 子组件 */
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_approval_edit],
  components: {},
  data() {
    return {
      moduleTitle: '任务关闭',
      urlPrefix: '/mpm/projectTaskClose/',
      url: {
        // 额外的url
        queryCodeList: '/mpm/projectTask/dataCodeList',
        queryTaskInfo: '/mpm/projectTask/list'
      },
      disableAttachmentButton: true
    }
  },
  props: {
    newValue: {
      type: Object,
      default: () => {}
    }
  },
  watch: {
    newValue: {
      handler(value, oldvalue) {
        this.model = value
        if (!this.model.hasOwnProperty('id')) {
          this.isAdd = true
          this.disableEditSubmit = false
        } else {
          this.isAdd = false
          this.disableEditSubmit = false
        }
        this.loaded()
      },
      immediate: true,
      deep: true
    }
  },
  computed: {
    isFinish() {
      return !(this.model.taskschedule === 100)
    },
    canDelete() {
      return validateNull(this.model.id)
    }
  },
  methods: {
    // 格式化日期 yyyy-MM-dd
    dateFormat(row, column, cellValue, index) {
      let dateValue = row[column.property]
      if (dateValue && dateValue.length > 0) {
        return formatDate(new Date(dateValue.replace(new RegExp(/-/gm), '/')), 'yyyy-MM-dd')
      } else {
        return ''
      }
    },
    formatDate(date) {
      return validateNull(date) ? '' : '于' + formatDate(new Date(date ? date.toString().replace(/\-/g, '/') : date), 'yyyy-MM-dd HH:mm')
    },
    changetaskschedule() {
      if (this.model.taskschedule === 100) {
        // this.isFinish = false
      } else {
        // this.isFinish = true
        this.model.taskactualenddate = null
      }
    },
    loaded() {
      this.disableAttachmentButton = true
      if (this.isAdd === false && this.disableEditSubmit === true) {
        var params = {
          sourceType: 'MpmProjectTaskClose',
          sourceId: this.model.id
        }
        this.disableAttachmentButton = false
        getAction('/cbo/attachment/calculateCount', params).then((res) => {
          if (res.success) {
            if (res.result === 0) {
              this.disableAttachmentButton = true
            }
          }
        })
      } else if (this.isAdd === false && this.disableEditSubmit === false) {
        this.disableAttachmentButton = false
      }
      // 页面加载完毕执行
      if (!this.isAdd && this.model.docno) {
        this.title = this.title + `（单据编号：${this.model.docno}）`
      }
    },
    afterDelApproval() {
      this.disableAttachmentButton = false
    },
    /** 执行新增提交 */
    doAdd() {
      this.saveLoading = true
      // 增加唯一单号
      postAction(this.url.add + '?billuid=' + this.uid, this.model)
        .then((res) => {
          if (res.success) {
            this.$tap.message('添加成功', 'success')
            // 对返回的日期进行处理
            this.model = res.result
            this.disableAttachmentButton = false
            this.isAdd = false
          } else {
            this.$tap.alert(res.message, 'warning')
          }
          this.afterSubmit()
          this.saveLoading = false
        })
        .catch((err) => {
          this.saveLoading = false
        })
    },
    /** 提交审批 */
    doSubmitApproval() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (!this.url.submit) {
            this.$tap.alert('请设置审批地址!', 'warning')
            return
          }
          this.$set(this.model, 'taskstate', '4')
          if (!this.beforeApproval()) {
            return
          }
          this.submitLoading = true
          this.$tap
            .confirm('确定提交申请吗？')
            .then((_) => {
              putAction(this.url.submit, this.model).then((res) => {
                if (res.success) {
                  this.$tap.message(res.message, 'success')
                  this.afterApproval()
                  this.visible = false
                  this.$emit('refresh')
                } else {
                  this.$tap.alert(res.message, 'warning')
                }
                this.submitLoading = false
              })
            })
            .catch((_) => {
              this.submitLoading = false
            })
        }
      })
    },
    setState() {
      if (this.model.taskstate === '2') {
        this.$set(this.model, 'taskschedule', 100)
      }
    },
    // 当点击删除按钮时，传递事件，删除feedbackList中新增内容
    deleteFeedbackList() {
      this.$emit('deleteList')
    }
  }
}
</script>
<style lang="scss" scoped>
.taskForm {
  margin-top: 15px;
  margin-left: -50px;
  .el-row {
    margin-bottom: 2px;
  }
}
</style>
